var firewarn = false;

$(document).ready(function(){
    $(window).bind('beforeunload', function(){
        if(firewarn){
            return 'There are either unsaved changes or busy operations running. Are you sure you want to leave?';
        }
    });

    $(window).unload(function(){
        if(firewarn){
            $.ajax({
                type: "POST",
                async:false,
                url: "Cashmate/submit-dccr",
                data: "method=cancel",
                context: document.body,
                success:function(msg){
                    return false;
                }
            })
        }
    });
                
    $("h2.menuItem").hover(function(){
        $(this).addClass("ui-widget-content").removeClass("ui-state-default");
        $(this).animate({
            "marginTop": "+=6px"
        }, "fast");
    },function (){
        $(this).animate({
            "marginTop": "-=6px"
        }, "fast");
        $(this).removeClass("ui-widget-content").addClass("ui-state-default");
    });
                
    $("h2.menuItem").click(function (){
        var token = $(this).attr("id").split("_");
        setEnable(token[0]);
    });
                
    $("div.theContent").hide();
    
    $("div#dccrContent").show();
})
            
function setEnable(report){
    if(report == "dccr"){
        $("div.theContent").fadeOut("fast", function (){
            $("div#dccrContent").fadeIn("fast");
        });
        $("span#level1").html("Daily Cash Collection Report");
    }else if(report == "weekly"){
        $("div.theContent").fadeOut("fast", function (){
            $("div#weekContent").fadeIn("fast");
        });
        $("span#level1").html("Weekly Performance Rating");
    }
};
            
/////////////////////////////////////////////////////////////////DCCR
/////////////////////////////////////////////////////////////////DCCR
$.subscribe('searchDccrGrid', function(event,data) {
    $("#dccrGrid").jqGrid('searchGrid', {
        drag:true,
        closeAfterSearch:false,
        closeOnEscape:true,
        sopt:['eq'],
        caption:"Search D.C.C.R"
    } );
});
    
$.subscribe("dccrGridComplete",function(event,data){
    $( "#dccrGrid_search" ).button( "option", "disabled", false );
    $( "#dialogDccrButton" ).button( "option", "disabled", false );
    $( "#reloadButton" ).button( "option", "disabled", false );
    $( "#exportButton" ).button( "option", "disabled", false );
});
    
$.subscribe("reloadgrid",function(event,data){
    var postdata = $("#dccrGrid").jqGrid('getGridParam','postData');
    postdata._search = false;
    postdata.searchField = "";
    postdata.searchOper = "";
    postdata.searchString = "";
    $("#dccrGrid").trigger("reloadGrid",[{
        page:1
    }]);
});
 
$.subscribe("celleditsuccess",function(event,data){
    $("#dccrGrid").jqGrid('setGridParam',{
        search:false
    });
    var postData = $("#dccrGrid").jqGrid('getGridParam','postData');
    $.extend(postData,{
        searchField:"",
        searchString:"",
        searchOper:""
    });
    $.extend(postData,{
        filters:""
    });
    $("#dccrGrid").trigger("reloadGrid");
});
$.subscribe("chooseBefore",function(event,data){
    var form = event.originalEvent.form[0];
    var datesplit;
    var date = new Date();
    var inputDate = new Date();
    var m;
    var d;
    var y;
    try{
        $('#chooseDCCRForm label[for=dccrdate]').css("color","black");
        $('#chooseDCCRForm label[for=branchselect]').css("color","black");
        datesplit = form.dccrdate.value.split("/");
        m = parseInt(datesplit[0],10);
        d = parseInt(datesplit[1],10);
        y = parseInt(datesplit[2],10);
        inputDate.setFullYear(y,m-1,d);
    //alert(date.getDate()+":"+d+","+(date.getMonth()+1)+":"+m+","+date.getFullYear()+":"+y);
    }catch(err){
        $('div#formResult').removeClass("ui-state-highlight");
        $('div#formResult').addClass("ui-state-error");
        $('div#formResult').html(error('Please enter a <strong> valid date in (mm/dd/yyyy) format</strong>'));
        $('#chooseDCCRForm label[for=dccrdate]').css("color","red");
        event.originalEvent.options.submit = false;
    }
    if(datesplit.length < 3){
        $('div#formResult').removeClass("ui-state-highlight");
        $('div#formResult').addClass("ui-state-error");
        $('div#formResult').html(error('Please enter a <strong> valid date.</strong>'));
        $('#chooseDCCRForm label[for=dccrdate]').css("color","red");
        event.originalEvent.options.submit = false;
    }else if(m > 12 || m < 1){
        $('div#formResult').removeClass("ui-state-highlight");
        $('div#formResult').addClass("ui-state-error");
        $('div#formResult').html(error('Please enter a <strong> valid month.</strong>'));
        $('#chooseDCCRForm label[for=dccrdate]').css("color","red");
        event.originalEvent.options.submit = false;
    }else if(d > 31 || d < 1){
        $('div#formResult').removeClass("ui-state-highlight");
        $('div#formResult').addClass("ui-state-error");
        $('div#formResult').html(error('Please enter a <strong> valid day.</strong>'));
        $('#chooseDCCRForm label[for=dccrdate]').css("color","red");
        event.originalEvent.options.submit = false;
    }else if(datesplit[2].length != 4){
        $('div#formResult').removeClass("ui-state-highlight");
        $('div#formResult').addClass("ui-state-error");
        $('div#formResult').html(error('Please enter a <strong> valid year.</strong>'));
        $('#chooseDCCRForm label[for=dccrdate]').css("color","red");
        event.originalEvent.options.submit = false;
    }else if(form.branchselect.value == "-1"){
        $('div#formResult').removeClass("ui-state-highlight");
        $('div#formResult').addClass("ui-state-error");
        $('div#formResult').html(error('Please select a <strong> branch.</strong>'));
        $('#chooseDCCRForm label[for=branchselect]').css("color","red");
        event.originalEvent.options.submit = false;
    }else{
        firewarn=true;
        $('#chooseDCCRForm').fadeOut();
        $('.dialogbutton').hide();
        $("#indicator").show();
        $('div#formResult').removeClass("ui-state-error");
        $('div#formResult').addClass("ui-state-highlight");
        $('div#formResult').html(info('Please wait. D.C.C.R. computation may take a minute or two...'));
                    
    }
});
$.subscribe("noClick",function(event,data){
    firewarn=false;
    $('div#formResult').removeClass("ui-state-error");
    $('div#formResult').addClass("ui-state-highlight");
    $('#formResult').html(info('Please <strong>fill-up the field(s) below to generate a Daily Cash Collection Report.</strong>'));
    $('#confirmGen').fadeOut();
    $('#indicator').hide();
    $('#chooseDCCRForm').fadeIn();
    $('.dialogbutton').fadeIn()                
});
$.subscribe("yesClickBefore",function(event,data){
    $('div#formResult').removeClass("ui-state-error");
    $('div#formResult').addClass("ui-state-highlight");
    $('div#formResult').html(info('Please wait. D.C.C.R. computation may take a minute or two...'));
    firewarn=true;
    $('#confirmGen').fadeOut('fast',function (){
        $('#indicator').show();
    });
                
});
$.subscribe("cancelSubmitClick",function(event,data){
    firewarn=false;
    $('#dccrDialog').dialog('close');
});
$.subscribe("dialogopen",function(event,data){
    $('.ui-dialog-titlebar-close').hide();
});
            
/////////////////////////////////////////////////////////////////WEEK
/////////////////////////////////////////////////////////////////WEEK
$.subscribe("cancelweekSubmitClick",function(event,data){
    $('#weekPerfDialog').dialog('close');
});
            
$.subscribe("weekperformanceGridComplete",function(event,data){
    $( "#weekPerfGrid_search" ).button( "option", "disabled", false );
});
            
$.subscribe('searchWeekGrid', function(event,data) {
    $("#performanceGrid").jqGrid('searchGrid', {
        drag:true,
        closeAfterSearch:false,
        closeOnEscape:true,
        sopt:['eq'],
        caption:"Search Weekly Performance"
    } );
});
            
$.subscribe('showHideCols', function(event,data) {
    $("#performanceGrid").jqGrid('setColumns',{});
});
            
$.subscribe('cutoffLoadComplete', function(event,data) {
    var s = $("#cutoffSelect").attr("value");
    if(s == "-1"){
        $('div#weekformResult').removeClass("ui-state-highlight");
        $('div#weekformResult').addClass("ui-state-error");
        $('div#weekformResult').html(error('Sorry.There are <strong>no available Daily Cash Collection Report(s)</strong> to compute.'));
        $('#weekSubmitButton').button( "option", "disabled", true );
    }
});